#!/bin/bash

USAGE="usage: $0 BUILD_SYSTEM=[android|stelp] PRODUCT=[ste_brdg|blaze|u9500|u8500|u5500]"

#Sanity check of arguments
if [ $# != 2 ]; then
    echo $USAGE
    exit
fi

# split key=value into two separate key and value parameters
until [ -z $1  ]; do
  OPTLEN=`expr length "$1"`
  KEYPOS=`expr index "$1" =`
  KEY=`expr substr "$1" 1 $((KEYPOS - 1))`
  VALUE=`expr substr "$1" $((KEYPOS + 1)) $OPTLEN`
  shift

  case $KEY in
    BUILD_SYSTEM )
      BUILD_SYSTEM=$VALUE ;;
    PRODUCT )
      PRODUCT=$VALUE ;;
  esac
done

MODULE_NAME="mid"
mkdir -p ../prebuilt
mkdir -p ../prebuilt/$MODULE_NAME
mkdir -p ../prebuilt/$MODULE_NAME/etc
mkdir -p ../prebuilt/$MODULE_NAME/lib

case $BUILD_SYSTEM in
  android )
    ROOT="../../../.."
#    Original
#    ROOT="../../../../.."
    PRODUCT_DIR="${ROOT}/out/target/product/${PRODUCT}"
    EXE="${PRODUCT_DIR}/obj/EXECUTABLES/${MODULE_NAME}_intermediates/mid"
    LIB_MOLIU5xx="${PRODUCT_DIR}/obj/SHARED_LIBRARIES/libmoliU5xx_intermediates/LINKED/libmoliU5xx.so"
    LIB_MOLIU33x="${PRODUCT_DIR}/obj/SHARED_LIBRARIES/libmoliU33x_intermediates/LINKED/libmoliU33x.so"
    LIB_AT="${PRODUCT_DIR}/obj/SHARED_LIBRARIES/libat_intermediates/LINKED/libat.so"
    LIB_USB="${PRODUCT_DIR}/obj/SHARED_LIBRARIES/libusb_intermediates/LINKED/libusb.so"
    LIB_APR1="${PRODUCT_DIR}/obj/SHARED_LIBRARIES/libapr-1_intermediates/LINKED/libapr-1.so"
    LIB_CONFIG="${PRODUCT_DIR}/obj/SHARED_LIBRARIES/libconfig_intermediates/LINKED/libconfig.so"
    ETC_FLASHED="${PRODUCT_DIR}/obj/ETC/Flashed_mid.conf_intermediates/Flashed_mid.conf"
    ETC_FLASHLESS="${PRODUCT_DIR}/obj/ETC/Flashless_mid.conf_intermediates/Flashless_mid.conf"

    if [ ! -d $PRODUCT_DIR ]; then
      echo "Product dir ${PRODUCT_DIR} not found!"
      exit
    fi

    cp -v ${EXE} ../prebuilt/${MODULE_NAME}
    cp -v ${LIB_MOLIU5xx} ../prebuilt/${MODULE_NAME}/lib
    cp -v ${LIB_MOLIU33x} ../prebuilt/${MODULE_NAME}/lib
    cp -v ${LIB_AT} ../prebuilt/${MODULE_NAME}/lib
    cp -v ${LIB_USB} ../prebuilt/${MODULE_NAME}/lib
    cp -v ${LIB_APR1} ../prebuilt/${MODULE_NAME}/lib
    cp -v ${LIB_CONFIG} ../prebuilt/${MODULE_NAME}/lib
    cp -v ${ETC_FLASHED} ../prebuilt/${MODULE_NAME}/etc
    cp -v ${ETC_FLASHLESS} ../prebuilt/${MODULE_NAME}/etc
    cp -v Android_prebuilt.mk ../prebuilt/${MODULE_NAME}/Android.mk

    rm -v ${PRODUCT_DIR}/system/bin/${MODULE_NAME}
    rm -v ${PRODUCT_DIR}/symbols/system/bin/${MODULE_NAME}
    rm -rfv ${PRODUCT_DIR}/obj/EXECUTABLES/${MODULE_NAME}_intermediates

    rm -v ${PRODUCT_DIR}/system/lib/libmoliU5xx.so
    rm -v ${PRODUCT_DIR}/symbols/system/lib/libmoliU5xx.so
    rm -rfv ${PRODUCT_DIR}/obj/SHARED_LIBRARIES/libmoliU5xx_intermediates

    rm -v ${PRODUCT_DIR}/system/lib/libmoliU33x.so
    rm -v ${PRODUCT_DIR}/symbols/system/lib/libmoliU33x.so
    rm -rfv ${PRODUCT_DIR}/obj/SHARED_LIBRARIES/libmoliU33x_intermediates

    rm -v ${PRODUCT_DIR}/system/lib/libat.so
    rm -v ${PRODUCT_DIR}/symbols/system/lib/libat.so
    rm -rfv ${PRODUCT_DIR}/obj/SHARED_LIBRARIES/libat_intermediates

    rm -v ${PRODUCT_DIR}/system/lib/libusb.so
    rm -v ${PRODUCT_DIR}/symbols/system/lib/libusb.so
    rm -rfv ${PRODUCT_DIR}/obj/SHARED_LIBRARIES/libusb_intermediates

    rm -v ${PRODUCT_DIR}/system/lib/libapr-1.so
    rm -v ${PRODUCT_DIR}/symbols/system/lib/libapr-1.so
    rm -rfv ${PRODUCT_DIR}/obj/SHARED_LIBRARIES/libapr-1_intermediates

    rm -v ${PRODUCT_DIR}/system/lib/libconfig.so
    rm -v ${PRODUCT_DIR}/symbols/system/lib/libconfig.so
    rm -rfv ${PRODUCT_DIR}/obj/SHARED_LIBRARIES/libconfig_intermediates

    chmod a+w ${PRODUCT_DIR}/system/etc/Flashed_mid.conf
    rm -v ${PRODUCT_DIR}/system/etc/Flashed_mid.conf
    rm -rfv ${PRODUCT_DIR}/obj/ETC/Flashed_mid.conf_intermediates

    chmod a+w ${PRODUCT_DIR}/system/etc/Flashless_mid.conf
    rm -v ${PRODUCT_DIR}/system/etc/Flashless_mid.conf
    rm -rfv ${PRODUCT_DIR}/obj/ETC/Flashless_mid.conf_intermediates
    ;;
  stelp )
	echo "Prebuilding for STELP is not yet supported"
	exit
    ;;
  * )
    echo "Unknown option BUILD_SYSTEM=${BUILD_SYSTEM}"
    echo $USAGE
    exit
    ;;
esac
